/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import models.*;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import service.CourseDao;
import service.ForumDao;
import service.HibernateUtil;
import service.UserDao;

/**
 *
 * @author kjeld
 */
public class ForumController extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        boolean loggedin = false;
        long userID = 0;
        for (Cookie cookie : request.getCookies()) {
            if (cookie.getName().equals("status")) {
                if (cookie.getValue().length() > 0) {
                    loggedin = true;
                }
            } else if (cookie.getName().equals("userID")) {
                userID = Long.parseLong(cookie.getValue());
            }
        }
        if(loggedin == true){
        HttpSession session = request.getSession(true);
        HibernateUtil HU = new HibernateUtil();
        Session sessie = HU.openSession();

        try {
            long courseID = 0;
            long questionID = 0;
            if (request.getParameter("courseID") != null) {
                courseID = Long.parseLong(request.getParameter("courseID"));
            }
            if (request.getParameter("questionID") != null) {
                questionID = Long.parseLong(request.getParameter("questionID"));
            }
            CourseDao cdao = new CourseDao(sessie);
            if (courseID == 0) {
                List<Course> courses = cdao.findAll();
                request.setAttribute("courses", courses);
                System.out.println("courses: " + courses.get(0).getName());
            } else {
                Course course = cdao.get(courseID);
                request.setAttribute("course", course);
                Set<ForumQuestion> questions = course.getForumQuestions();
                if(questionID == 0){
                    request.setAttribute("questions",questions);
                } else {
                    ForumDao fdao = new ForumDao(sessie);
                    ForumQuestion question = fdao.get(questionID);
                    request.setAttribute("questionID", questionID);
                    request.setAttribute("question",question);
                    
                    //Criteria answersCrit = sessie.createCriteria(ForumAnswer.class).add(Restrictions.in("questionID", new String[]{questionID + ""}));
                    //List answers = answersCrit.list();
                    Set<ForumAnswer> answers = question.getAnswers();
                    //Set<ForumAnswer> answers = question.getForumAnswers();
                    request.setAttribute("answers",answers);
                }
            }
            RequestDispatcher rd = request.getRequestDispatcher("forum.jsp");
            rd.forward(request, response);
        } catch (Exception exception) {
            System.out.println(exception);
        }
        } else {
            response.sendRedirect("/");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        long questionID = 0;
        if(request.getParameter("questionID") != null){
            questionID = Long.parseLong(request.getParameter("questionID"));
        }
        long userID = 0;
        for (Cookie cookie : request.getCookies()) {
            if (cookie.getName().equals("userID")) {
                userID = Long.parseLong(cookie.getValue());
            }
        }
        if(questionID != 0){
            HibernateUtil HU = new HibernateUtil();
            Session sessie = HU.openSession();
            UserDao udao = new UserDao(sessie);
            User user = udao.get(userID);
            String answerString = request.getParameter("answerPost");
            Date date = new Date();
            ForumAnswer answer = new ForumAnswer(answerString, date);
            answer.setAnswerPoster(user);
            ForumDao fdao = new ForumDao(sessie);
            ForumQuestion question = fdao.get(questionID);
            fdao.saveAnswer(answer);
            question.addAnswer(answer);
            fdao.update(question);
        }
    }
}
